<?php

class MysqlHelper{
	private $location;
	private $username;
	private $password;
	private $database;

	private $connection;
	private $query;
	
	private $tablePrefix;

	function __construct(MysqlConfig $MySqlConfig){
		$Config = $MySqlConfig->getConfig();
		$this->setConfiguration($Config["location"],$Config["username"],$Config["password"],$Config["database"], $Config["table_prefix"]);
	}
	
	function setConfiguration($location, $username, $password, $database, $tablePrefix){
		$this->location = $location;
		$this->username = $username;
		$this->password = $password;
		$this->database = $database;
		$this->tablePrefix = $tablePrefix;
	}
	
	function connect(){
			$this->connection = mysql_connect($this->location, $this->username, $this->password);
			if(!$this->connection){
				die("Can't connect to database:<br />\n".mysql_error());
			}
			
			$database = mysql_select_db($this->database, $this->connection);
			if(!$database){
				die("Can't select database:<br />\n".mysql_error());
			}
			$this->query("SET NAMES UTF8");
		
		return $this->connection;
	}
	
	function query($sql){
		if($this->connection == null){
			$this->connect();
		}
		
		$this->query = @mysql_query($sql);
		if(!$this->query){
			die("Query failed.<br />\n".mysql_error()."<br />\n".mysql_errno()."<br />\n<b>Your query:</b> ".$sql);
		}
		
		return $this->query;
	}
	
	/*
		@param string	table
		@param array 	conditions	: array("category_id" => 5, "author_id" => 3)	equals WHERE category_id = 5 AND author_id = 3
		@param string	order		: "id desc" 									equals ORDER BY id DESC
		@param int/str	limit	: array(10,20)	equals LIMIT 10,20 ; 5 equals LIMIT 0,5
	*/
	function generateSelectQuery($table, array $conditions = null, $order = null, $limit = null){
		/* Generate SQL */
		$statement = "SELECT * FROM $table";

		$where_sql = null;
		if(is_array($conditions)){
			$loop = 1;
			foreach ($conditions as $field => $value){
				if(count($conditions) == 1 || $loop == 1){
					$where_sql .= " WHERE $field = '$value'";
				}
				else {
					$where_sql .= " AND $field = '$value'";
				}
			$loop++;
			}
		}
		
		$order_sql = null;
		if(is_string($order)){
			$order_sql = " ORDER BY ".$order; 
		}
		
		$limit_sql = "";
		if(is_array($limit)){
			$limit_sql = " LIMIT ".$limit[0].", ".$limit[1];
		}
		elseif(is_integer($limit)) {
			$limit_sql = " LIMIT ".$limit;
		}
		
		$statement .= $where_sql.$order_sql.$limit_sql;
		
		return $statement;
	}
	
	function generateUpdateQuery($table, array $fieldsValues, array $conditions, $ignoreNull = false){
		$statement = "UPDATE $table SET ";
		
		// generate fields & values to update
		$fieldsAndValues_sql = "";
		if(count($fieldsValues) > 0){
			$loop = 1;
			
			foreach ($fieldsValues as $field => $value){
				if($ignoreNull != false){
					if($value != null){
						$fieldsAndValues_sql .= ($loop == 1) ? $field." = '".$value."'" : ", ".$field." = '".$value."'";
					}
				}
				else {
					$fieldsAndValues_sql .= ($loop == 1) ? $field." = '".$value."'" : ", ".$field." = '".$value."'";
				}
				
				$loop++;
			}
		}
		
		// generate condition
		$where_sql = null;
		if(is_array($conditions)){
			$loop = 1;
			foreach ($conditions as $field => $value){
				if(count($conditions) == 1 || $loop == 1){
					$where_sql .= " WHERE $field = '$value'";
				}
				else {
					$where_sql .= " AND $field = '$value'";
				}
			$loop++;
			}
		}
		
		$statement .= $fieldsAndValues_sql.$where_sql;
		
		return $statement;
	}
	
	function getPrefix(){
		return $this->tablePrefix;
	}

	function __destruct(){
		if($this->connection != null){
			@mysql_close($this->connection);
			$this->connection = null;
		}
	}
}

?>